package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.mappers.SmsCouponHistoryMapper;
import com.mall.dubbo.mappers.SmsCouponMapper;
import com.mall.dubbo.mappers.SmsCouponProductCategoryRelationMapper;
import com.mall.dubbo.mappers.SmsCouponProductRelationMapper;
import com.tanhua.model.domain.SmsCoupon;
import com.tanhua.model.domain.SmsCouponHistory;
import com.tanhua.model.domain.SmsCouponProductCategoryRelation;
import com.tanhua.model.domain.SmsCouponProductRelation;
import com.tanhua.model.vo.CouponIdVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsCouponApiImpl implements SmsCouponApi{
    @Autowired
    private SmsCouponMapper smsCouponMapper;

    @Autowired
    private SmsCouponProductCategoryRelationMapper smsCouponProductCategoryRelationMapper;

    @Autowired
    private SmsCouponProductRelationMapper smsCouponProductRelationMapper;

    @Autowired
    private SmsCouponHistoryMapper smsCouponHistoryMapper;

    @Autowired
    private SmsCouponHistoryApi smsCouponHistoryApi;

    @Override
    public Page couponList(String name, Integer type, Page<SmsCoupon> page) {
        LambdaQueryWrapper<SmsCoupon> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null,SmsCoupon::getName,name)
                .eq(type != null,SmsCoupon::getType,type);
        return smsCouponMapper.selectPage(page,lqw);
    }


    /**
     *根据id获取单个优惠券的详细信息
     * @param id
     * @return
     */
    @Override
    public CouponIdVo findOneById(Long id) {
        SmsCoupon smsCoupon = smsCouponMapper.selectById(id);
        if (smsCoupon == null){
            return null;
        }
        CouponIdVo couponIdVo = new CouponIdVo();
        BeanUtils.copyProperties(smsCoupon,couponIdVo);


        LambdaQueryWrapper<SmsCouponProductCategoryRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null,SmsCouponProductCategoryRelation::getCouponId,id);
        List<SmsCouponProductCategoryRelation> categoryRelationList = smsCouponProductCategoryRelationMapper.selectList(lqw);

        LambdaQueryWrapper<SmsCouponProductRelation> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(id != null,SmsCouponProductRelation::getCouponId,id);
        List<SmsCouponProductRelation> productRelationList = smsCouponProductRelationMapper.selectList(lqw2);

        couponIdVo.setProductCategoryRelationList(categoryRelationList);
        couponIdVo.setProductRelationList(productRelationList);

        //通过sms_coupon_history表 核验领取数量, 使用数量
        //发行总数量
        Integer publishCount = smsCoupon.getPublishCount();
        //查询领取数量
        Integer receiveCount = smsCouponHistoryApi.findByCouponIdStatusCount(id, null);
        //查询使用数量
        Integer useCount = smsCouponHistoryApi.findByCouponIdStatusCount(id, 1);

        smsCoupon.setReceiveCount(receiveCount);
        smsCoupon.setUseCount(useCount);
        //待领取的数量
        smsCoupon.setCount(publishCount-receiveCount);


        //更新数据库的数量
        SmsCoupon smsCoupon1 = new SmsCoupon();
        smsCoupon1.setId(id);
        smsCoupon1.setReceiveCount(receiveCount);
        smsCoupon1.setUseCount(useCount);
        smsCoupon1.setCount(smsCoupon.getCount());
        smsCouponMapper.updateById(smsCoupon1);

        return couponIdVo;
    }

    /**
     * 添加优惠券 sms_coupon sms_coupon_product_category_relation  sms_coupon_product_relation
     * @param couponIdVo
     * @return
     */
    @Override
    public void create(CouponIdVo couponIdVo) {
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(couponIdVo,smsCoupon);
        smsCouponMapper.insert(smsCoupon);

        List<SmsCouponProductRelation> productRelationList = couponIdVo.getProductRelationList();
        if (productRelationList != null){
            for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelation.setCouponId(smsCoupon.getId());
                smsCouponProductRelationMapper.insert(smsCouponProductRelation);
            }
        }

        List<SmsCouponProductCategoryRelation> productCategoryRelationList = couponIdVo.getProductCategoryRelationList();
        if (productCategoryRelationList != null){
            for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelation.setCouponId(smsCoupon.getId());
                smsCouponProductCategoryRelationMapper.insert(smsCouponProductCategoryRelation);
            }
        }
    }

    @Override
    public void update(CouponIdVo couponIdVo) {

        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(couponIdVo,smsCoupon);
        smsCouponMapper.updateById(smsCoupon);

        //先将 sms_coupon_product_category_relation  sms_coupon_product_relation 删除
        LambdaQueryWrapper<SmsCouponProductRelation> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(smsCoupon.getId() != null,SmsCouponProductRelation::getCouponId,smsCoupon.getId());
        smsCouponProductRelationMapper.delete(lqw1);

        LambdaQueryWrapper<SmsCouponProductCategoryRelation> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(smsCoupon.getId() != null,SmsCouponProductCategoryRelation::getCouponId,smsCoupon.getId());
        smsCouponProductCategoryRelationMapper.delete(lqw2);

        //再添加
        List<SmsCouponProductRelation> productRelationList = couponIdVo.getProductRelationList();
        if (productRelationList != null){
            for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelation.setCouponId(smsCoupon.getId());
                smsCouponProductRelationMapper.insert(smsCouponProductRelation);
            }
        }

        List<SmsCouponProductCategoryRelation> productCategoryRelationList = couponIdVo.getProductCategoryRelationList();
        if (productCategoryRelationList != null){
            for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelation.setCouponId(smsCoupon.getId());
                smsCouponProductCategoryRelationMapper.insert(smsCouponProductCategoryRelation);
            }
        }

    }


    /**
     * 删除优惠卷
     */
    @Override
    public void delete(Long id) {
        //删除smsCoupon
        smsCouponMapper.deleteById(id);

        //先将 sms_coupon_product_category_relation  sms_coupon_product_relation 删除
        LambdaQueryWrapper<SmsCouponProductRelation> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(id != null,SmsCouponProductRelation::getCouponId,id);
        smsCouponProductRelationMapper.delete(lqw1);

        LambdaQueryWrapper<SmsCouponProductCategoryRelation> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(id != null,SmsCouponProductCategoryRelation::getCouponId,id);
        smsCouponProductCategoryRelationMapper.delete(lqw2);

        //删除sms_coupon_history表数据
        LambdaQueryWrapper<SmsCouponHistory> lqw3= new LambdaQueryWrapper<>();
        lqw3.eq(id != null,SmsCouponHistory::getCouponId,id);
        smsCouponHistoryMapper.delete(lqw3);
    }
}
